﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace KgUtils.IO
{
    public static partial class FileHelper
    {
        /// <summary>
        /// Send the specified file to the user.
        /// If the download is successful, the response ends here !
        /// </summary>
        /// <param name="response">The HttpResponse associated to the current page</param>
        /// <param name="file">The file to send to the user</param>
        /// <returns>If the download is successful, no return as the response ends. Else, returns false.</returns>
        public static bool StartDownloadFile(HttpResponse response, FileInfo file)
        {
            if (file.Exists)
            {
                response.Clear();
                response.AddHeader("Content-Disposition", String.Concat("attachment; filename=\"", file.Name, "\""));
                response.AddHeader("Content-Length", file.Length.ToString());
                response.ContentType = "application/octet-stream";
                response.TransmitFile(file.FullName);
                response.End();
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Send the specified file to the user.
        /// If the download is successful, the response ends here !
        /// </summary>
        /// <param name="response">The HttpResponse associated to the current page</param>
        /// <param name="file">The file to send to the user</param>
        /// <returns>If the download is successful, no return as the response ends. Else, returns false.</returns>
        public static bool StartDownloadFile(HttpResponse response, string fileName, Stream stream)
        {
            if (stream.Length > 0)
            {
                response.Clear();
                response.Buffer = false;
                response.AddHeader("Content-Disposition", String.Concat("attachment; filename=\"", fileName, "\""));
                response.AddHeader("Content-Length", stream.Length.ToString());
                response.ContentType = "application/octet-stream";

                byte[] buffer = new byte[1 << 16]; // 64kb
                int bytesRead = 0;
                while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    response.OutputStream.Write(buffer, 0, bytesRead);
                }
                response.End();

                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Checks that the string doesn't contain invalid filename char.
        /// Returns true for null or empty strings.
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool IsFileNameValid(string filename)
        {
            if (filename.IsNullOrEmpty())
                return true;

            foreach (var invalidChar in Path.GetInvalidFileNameChars())
            {
                if (filename.Contains(invalidChar))
                    return false;
            }
            return true;
        }
    }
}
